import requests
import re

from django.core.files.base import ContentFile

from images import utils 


def upload_image(request, profile_form):

    """ Загружаем изображение на аватар пользователя """

    print(f'cleaned_data - {profile_form.cleaned_data}')

    if profile_form.cleaned_data['photo']:

        image_link = profile_form.cleaned_data['photo']
        image_extension = utils.get_image_extension(image_link)
        image_name = image_link.split(image_extension)[0].split('/')[-1] + image_extension
        image_content = requests.get(image_link).content
        request.user.profile.photo.save(name=image_name, content=ContentFile(image_content))
